package com.yqg.protection.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.EquipmentUnit;
import com.yqg.protection.mapper.EquipmentUnitMapper;
import com.yqg.protection.service.IEquipmentUnitService;
import com.yqg.protection.utils.Constants;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * EquipmentUnitController 前端控制器
 *
 * @author zxf
 * @since 2024-09-11
 */
@RestController
@RequestMapping("/equipment-unit")
public class EquipmentUnitController {

    @Resource
    private IEquipmentUnitService equipmentUnitService;

    @Resource
    private EquipmentUnitMapper equipmentUnitMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改设备部件")
    public Result save(@RequestBody EquipmentUnit equipmentUnit) {
        equipmentUnit.setIsDelete(0);
        LambdaQueryWrapper<EquipmentUnit> equipmentUnitQueryWrapper = new LambdaQueryWrapper<>();
        equipmentUnitQueryWrapper.eq(EquipmentUnit::getPartModel, equipmentUnit.getPartModel());
        equipmentUnitQueryWrapper.eq(EquipmentUnit::getEquipmentInputStoreId, equipmentUnit.getEquipmentInputStoreId());
        equipmentUnitQueryWrapper.eq(EquipmentUnit::getIsDelete, 0);
        EquipmentUnit equipmentUnit1 = equipmentUnitService.getOne(equipmentUnitQueryWrapper);
        if (ObjectUtil.isEmpty(equipmentUnit.getId())) {
            if (equipmentUnit1 != null) {
                return Result.error(Constants.CODE_500, "该传输设备下已存在该型号");
            }
        } else {
            if (equipmentUnit1 != null && !equipmentUnit.getId().equals(equipmentUnit1.getId())) {
                return Result.error(Constants.CODE_500, "该传输设备下已存在该型号");
            }
        }
        equipmentUnit.setWarnStateNum("0");
        return Result.success(equipmentUnitService.saveOrUpdate(equipmentUnit));
    }

    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    @SysLog("删除设备部件")
    public Result delete(@PathVariable Integer id) {
        EquipmentUnit equipmentUnit = equipmentUnitService.getById(id);
        equipmentUnit.setIsDelete(1);
        return Result.success(equipmentUnitService.saveOrUpdate(equipmentUnit));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<EquipmentUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<EquipmentUnit> list = equipmentUnitService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(equipmentUnitService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           Integer level,
                           Long organizationId,
                           String clientName,
                           String equipmentId,
                           String equipmentName,
                           String name,
                           String model,
                           String address,
                           Integer systemId,
                           Long typeId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<EquipmentUnit> equipmentUnits = equipmentUnitMapper.findPage(pageNum, pageSize, level, organizationId, clientName, equipmentId, equipmentName, name, model, address, systemId, typeId, isAllClient, clientIds);
        Long count = equipmentUnitMapper.findPageCount(level, organizationId, clientName, equipmentId, equipmentName, name, model, address, systemId, typeId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", equipmentUnits);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       Integer level,
                       Long organizationId,
                       String clientName,
                       String equipmentId,
                       String equipmentName,
                       String name,
                       String model,
                       String address,
                       Integer systemId,
                       Long typeId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        equipmentUnitService.export(response, level, organizationId, clientName, equipmentId, equipmentName, name, model, address, systemId, typeId, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入设备部件")
    public void importData(MultipartFile file) {
        equipmentUnitService.importData(file);
    }
}

